package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Commonweal;
import cn.xgj.sfzy.service.CommonwealService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CommonwealController
 * @Description TODO
 * @Author 李政阳
 * @Date 2024年10月25日 11:26
 * @Version 1.0
 */
@RestController
@RequestMapping("/commonweal")
public class CommonwealController {
    @Autowired
    private CommonwealService service;

    @PostMapping
    public Result add(@RequestBody Commonweal commonweal){
        int i = service.add(commonweal);
        if (i==0){
            return Result.error("添加失败");
        }else {
            return Result.success("添加成功");
        }
    }
    @DeleteMapping("/{commonwealId}")
    public Result delete(@PathVariable(value = "commonwealId") Integer id){
        int i = service.delete(id);
        if (i==0){
            return Result.error("添加失败");
        }else {
            return Result.success("添加成功");
        }
    }
    @PutMapping
    public Result update(@RequestBody Commonweal commonweal){
        int i = service.update(commonweal);
        if (i==0){
            return Result.error("添加失败");
        }else {
            return Result.success("添加成功");
        }
    }
    @GetMapping
    public Result findAll(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
            Commonweal commonweal){
        PageInfo<Commonweal> info = service.findByPage(pageNum, pageSize, commonweal);
        return Result.success(info);
    }
}
